Ext.ns('WaterCheckView');

WaterCheckView = Ext.extend(Ext.Panel, {
	// 头部工具栏
	topbar : null,
	// 条件搜索Panel
	searchPanel : null,
	// 数据展示Panel
	gridPanel : null,
	// GridPanel的数据Store
	store : null,
	// 构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		// 初始化组件
		this.initUIComponents();
		// 调用父类构造
		WaterCheckView.superclass.constructor.call(this, {
					id : 'WaterCheckView',
					title : '水表抄表记录审核',
					region : 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});
	},// end of constructor
	// 初始化组件
	initUIComponents : function() {
		// 初始化搜索条件Panel
		this.searchPanel = new Ext.FormPanel({
					id : 'WaterSearchForm',
					height : 70,
					region : 'north',
					frame : false,
					border : false,
					padding : '10',
					layout : 'column',
					title : '查询条件',
					labelWidth : 60,
					enctype : 'multipart/form-data',
					fileUpload : true,
					url : __ctxPath + '/pdaweb/planListPdaWaterList.do',
					layoutConfig : {
						padding : '5',
						align : 'middle'
					},
					defaults : {
						columnWidth : 0.33,
						layout : 'form',
						border : false,
						style : 'padding-left:10px;',
						defaults : {
							xtype : 'textfield',
							width : 100
						}
					},
					items : [{
								items : [{
											id:'month_show',
											fieldLabel : '抄表年月',
											xtype : 'monthfield',
											value : new Date(),
											editable : false,
											allowblank : false,
											name : 'Q_month_S_EQ'
										},{  
											id:'month_hide',
											xtype : 'hidden'
										}]
							}, {
								items : [{
											fieldLabel : '计划名称',
											xtype : 'textfield',
											width : 160,
											name : 'Q_planName_S_LK'
										}]
							}, {
								items : [{
											fieldLabel : '抄表人',
											xtype : 'textfield',
											name : 'Q_executor_S_LK'
										}]
							}]
				});// end of the searchPanel
		
		this.store = new Ext.data.JsonStore({
					url : __ctxPath + '/pdaweb/planListPdaWaterList.do',
					root : 'result',
					totalProperty : 'totalCounts',
					id : 'id',
					fields : ['id','planId', 'planName', 'executor',
						      'lastEditBy','planStatus'],
					remoteSort : true
		});

		var sm = new Ext.grid.CheckboxSelectionModel({
            handleMouseDown:Ext.emptyFn,
            listeners:{'beforerowselect': function(s,i,k,r ) {
                      return r.get("planStatus")!='2';
                }
            },
            renderer : function(v,c,r){
	            if(r.get("planStatus")=='2'){
	                return "<div style='padding:3px 0 0 3px;'><input type='checkbox' disabled /></div>";
	            }else{
	                return "<div class=\"x-grid3-row-checker\">&#160;</div>";
	            }
            }            
        });
		
		var cm = new Ext.grid.ColumnModel({
			columns : [sm, new Ext.grid.RowNumberer(), {
						header : '计划名称',
						width : 220,
						dataIndex : 'planName',
						renderer : showData	
					}, {
						header : '抄表人',
						width : 120,
						dataIndex : 'executor'
					}, {
						header : '抄表年月',
						width : 120,
						dataIndex : 'lastEditBy'
					}],
			defaults : {
				sortable : true,
				menuDisabled : false
			}
		});// end of the cm

		this.topbar = new Ext.Toolbar({
					id : 'WaterFootBar',
					height : 30,
					bodyStyle : 'text-align:left',
					items : [new Ext.Button({
								iconCls : 'search',
								text : '查询',
								handler : function() {
									Ext.getCmp('month_hide').setValue(Ext.getCmp('month_show').getValue().format('Y-m'));
									var searchPanel = Ext
											.getCmp('WaterSearchForm');
									var gridPanel = Ext.getCmp('WaterGrid');
									if (searchPanel.getForm().isValid()) {
										$search({
													searchPanel : searchPanel,
													gridPanel : gridPanel
												});
										var store = gridPanel.getStore();
										var count = store.getCount();
										for (var i = 0; i < count; i++) {
											if (store.getAt(i).get("planStatus") == "1") {
												sm.selectRow(i);
											}
										}
									}
								}
							})]
				});

		this.gridPanel = new Ext.grid.GridPanel({
					id : 'WaterGrid',
					region : 'center',
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					tbar : this.topbar,
					cm : cm,
					sm : sm,
					viewConfig : {
						showPreview : false,
						getRowClass : function(record, rowIndex, rowParams,store) {// 异常状态
							var st = record.data.planStatus;
							if ('ERROR' == WaterCheckView.getST(st)) {
								return 'x-grid-record-error';
							} else if ('WARN' == WaterCheckView.getST(st)) {
								return 'x-grid-record-warning';
							} else {
								return '';
							}
						}
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 15,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
				});

		if (isGranted('_WaterCheck')) {
			this.topbar.add(new Ext.Button({
				iconCls : 'btn-add',
				text : '审核',
				handler : function() {
					var grid = Ext.getCmp("WaterGrid");
					var selectRecords = grid.getSelectionModel()
							.getSelections();

					if (selectRecords.length == 0) {
						Ext.ux.Toast.msg("信息", "请选择记录！");
						return;
					}

					var warnIds = '';
					var waitIds = '';
					for (var i = 0; i < selectRecords.length; i++) {
						var st = selectRecords[i].data.planStatus;
						if ('WAIT' == WaterCheckView.getST(st)) {
							waitIds += selectRecords[i].data.planId + ',';
						} else if ('WARN' == WaterCheckView.getST(st)) {
							warnIds += selectRecords[i].data.planId + ',';
						}
					}
					if (warnIds != '') {
						Ext.Msg.confirm('提示', '选择中包含黄颜色警示记录,你确定要审核通过吗？',
								function(btn, text) {
									if (btn == 'yes') {
										WaterCheckView.check(waitIds+warnIds);
									} else {
										return false;
									}
								});
					} else {
						if (waitIds != '') {
							WaterCheckView.check(waitIds)
						}
					}
				}
			}));
		}
	}
});

// 编辑
WaterCheckView.edit = function(recId,recordStr) {
	new WaterForm({
				recId : recId,
                checkMode:true,
				recordStr : recordStr
			}).show();
}

/**
 * getStatusType
 * 
 * @param {}
 *            st status
 * @return {}
 */
WaterCheckView.getST = function(st) {
	return st == '0' ? 'WAIT' : st == '2' ? 'ERROR' : st == '3' || st == '4'
			? 'WARN'
			: 'OTHER';
}

//审核
WaterCheckView.check = function(ids) {
	var month = Ext.getCmp('month_hide').getValue();
	Ext.Ajax.request({
		url : __ctxPath
				+ '/pdaweb/checkPlanPdaWaterList.do?fileName='
				+ ids+"&Q_month_S_EQ="+month,
		waitMsg : '正在提交数据...',
		timeout: 30000,		
		method : 'post',
		success : function(response) {
			Ext.ux.Toast.msg("操作信息", "审核成功");
			Ext.getCmp('WaterGrid').getStore().reload();
		},
		failure : function() {
//			Ext.Msg.alert("操作信息", "审核失败");
			Ext.MessageBox.show({
				title : '操作信息',
				msg : '审核失败',
				buttons : Ext.MessageBox.OK,
				icon : 'ext-mb-error'
			});
		}
	});
}

//计划审核明细
function showData(value, cellmeta, record, rowIndex){
	return "<a href='#' onClick=WaterCheckView.detail('" + value
		+ "'," + "'" + record.data["planName"] + "','"
		+ record.data["executor"] + "'" + ",'"
		+ record.data["lastEditBy"] + "','"+record.data["planId"]+"')>" + value + "</a>";
	
}

//detail方法
//弹出水表审核界面
WaterCheckView.detail = function(id,planName,  executor, lastEditBy,planId) {
	new WaterCheckForm0({
		  	id : id,
			planName : planName,
			executor  : executor,
			lastEditBy : lastEditBy,
			planId:planId
		}).show();
};


